package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.UpLoad;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/product/update")
public class ProductUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");


        //通过upload拿取页面传过来的文件值
        UpLoad upLoad=new UpLoad();
        String string=upLoad.imgUpload(req);
        String products[]=string.split("@@",-1);
        ProductModel productModel=new ProductModel();

        String imgHref= (String) req.getSession().getAttribute("imgHref");
        if (products[2].equals("undefined")){
            products[2]=imgHref;
        }

        productModel.setId(Integer.parseInt(products[0]));
        productModel.setCasetypeid(Integer.parseInt(products[1]));
        productModel.setImgHref(products[2]);
        productModel.setContent(products[3]);


        //2.接收前台传数据过来rquserst




        IProductService iProductService=new ProductServiceImpl();

        int result=iProductService.update(productModel);

        //7.将数据转发到页面
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
}
